package zhaoche.gps.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import zhaoche.gps.model.CarGpsRecordModel;

public class GPSMessageUtil {

	static SimpleDateFormat df = new SimpleDateFormat("ddMMyyHHmmss");
	
	public GPSMessageUtil() {
		
	}
	
	public static List<CarGpsRecordModel> converToModel(String message)
    {
    	if(message.startsWith("*HQ"))
    	{
    		List<CarGpsRecordModel> models = new ArrayList<CarGpsRecordModel>();
    		String[] records = message.split("\\*");
    		for(int i=0;i<records.length;i++)
    		{
    			String[] gpsInfos = records[i].split(",");
    			if(gpsInfos.length==13)
    			{
    				String devId = gpsInfos[1];
    				String dateTimeStr = gpsInfos[11] + gpsInfos[3];
    				String lonStr = gpsInfos[7];
    				String latStr = gpsInfos[5];
    				String speedStr = gpsInfos[9];
    				String directionStr = gpsInfos[10];
    				String statusStr = gpsInfos[12];
    				String isGpsGoodStr = gpsInfos[4];
    				
    				Timestamp dateTime = null;
    				String lon = "";
    				String lat = "";
    				double speed = 0;
    				int direction = 0;
    				
    				try {
    					if(isGpsGoodStr.equals("A"))
    					{
    						Date date = df.parse(dateTimeStr);
        					Calendar cal = Calendar.getInstance();
        					cal.setTime(date);
        					cal.add(Calendar.HOUR_OF_DAY, 8);
        					dateTime = new Timestamp(cal.getTime().getTime());
    					}
    					else
    					{
    						dateTime = new Timestamp(Calendar.getInstance().getTime().getTime());
    					}
    					
    					lon = toLon(lonStr);
    					lat = toLat(latStr);
    					speed = Double.parseDouble(speedStr);
    					direction = Integer.parseInt(directionStr);
    					CarGpsRecordModel model = new CarGpsRecordModel(UUID.randomUUID().toString());
    					model.setDeviceNum(devId);
    					model.setDirection(direction);
    					model.setGpsrevTime(dateTime);
    					model.setLatitude(lat);
    					model.setLongitude(lon);
    					model.setVehicleStatus(statusStr);
    					model.setSpeed(speed);
    					models.add(model);
    					model.setIsGpsGood((isGpsGoodStr.equals("A")));
					} catch (Exception e) {
						return null;
					}
    			}
    		}
    		return models;
    	}
    	else
    	{
    		return null;
    	}
    }
	
	 private static String toLon(String lonStr)
     {
    	if(lonStr.length() == 10)
    	{
    		String result = "0";
    		String duStr = lonStr.substring(0, 3);
    		String fenStr = lonStr.substring(3, 9);
    		double fenDouble = 0;
    		double duDouble = 0;
    		
    			 duDouble = Double.parseDouble(duStr);
    			 fenDouble = Double.parseDouble(fenStr);
    			 fenDouble = fenDouble / 60;
    			 result = duDouble+fenDouble+""; 
			
    		return result;
    	}
    	else
    	{
    		return "0";
    	}
     }
	    
    private static String toLat(String latStr)
    {
    	if(latStr.length() == 9)
    	{
    		String result = "0";
    		String duStr = latStr.substring(0, 2);
    		String fenStr = latStr.substring(2, 8);
    		double fenDouble = 0;
    		double duDouble = 0;
    			 duDouble = Double.parseDouble(duStr);
    			 fenDouble = Double.parseDouble(fenStr);
    			 fenDouble = fenDouble / 60;
    			 result = duDouble+fenDouble+""; 
    		return result;
    	}
    	else
    	{
    		return "0";
    	}
    }

}
